#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# @Time    : 2018/11/18 18:50
# @Author  : liujiantao
# @Site    : 
# @File    : operation_ftr_extract.py
# @Software: PyCharm
import os

from tiancheng.base.base_helper import *
op_merge = pd.read_pickle(features_base_path+"op_merge01.pkl")

# label = op_merge[tag_header]
tag = get_tag_train_new()
UID_LIST = tag[tag_hd.UID].tolist()#57615
print(len(UID_LIST))
ftr_path = features_base_path + "op_merge_ftr_07.pkl"
ftr_list = []
# if os.path.exists(ftr_path):
#     ftr_list = pd.read_pickle(ftr_path).tolist()
#     ll = set([x[0] for x in ftr_list])
#     UID_LIST = list(set(UID_LIST).difference(ll))
#     print(len(UID_LIST))


def getf():
    save_path = features_base_path + "op_merge_v2.pkl"
    try:
        for i, uid in enumerate(UID_LIST):  # 17186
            print(i, uid)
            uid_data = op_merge[op_merge[op_hd.UID] == uid]
            df, outliers_rate, tag_uid = outliers_detection(uid_data)
            # ftr = get_feature(df, tag_uid)
            ftr = df.values.tolist()
            ftr.append(outliers_rate)
            ftr_list.append(ftr)
    except:
        traceback.print_exc()
        # pd.to_pickle(ftr_list, save_path)
    # value0 = [[row for row in ftr_list] for i in range(len(ftr_list[0]))]

    pd.to_pickle(ftr_list, save_path)

# getf(0,2864)
# getf(2864,2864*2)
# getf(2864*2,2864*3)
# getf(2864*3,2864*4)
# getf(2864*4,2864*5)
getf()
print(123)